package mx.itesm.gda.clever.ui;

import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.part.ViewPart;

public class ErrorsView extends ViewPart {
	
	public static final String ID = "mx.itesm.gda.clever.ui.ErrorsView";
	
	private static ErrorsView instance;
	
	public ErrorsView() {
		if (instance == null) instance = this;
	}
	
	private Table table;
	
	private String[] columnHeaders = {
			LocalizedMessages.ErrorsView_lineHeader,
			LocalizedMessages.ErrorsView_typeHeader,
			LocalizedMessages.ErrorsView_errorHeader
	};
	private int[] columnStyles = {
			SWT.RIGHT, SWT.LEFT, SWT.LEFT
	};
	private int[] columnWidths = {
			50, 100, 650
	};
	
	@Override
	public void createPartControl(final Composite parent) {
		
		// Set up the table.
		
		table = new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI
				| SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setForeground(new Color(table.getDisplay(), 255, 0, 0));
		
		for (int i = 0; i < columnHeaders.length; i++) {
			final TableColumn column = new TableColumn(table, columnStyles[i]);
			column.setText(columnHeaders[i]);
			column.setWidth(columnWidths[i]);
		}
	}
	
	@Override
	public void setFocus() {}
	
	public static void setErrors(final Map errors, boolean syntactic) {
		if (instance == null) return;
		
		instance.table.removeAll();
		addErrors(errors, syntactic);
	}
	
	public static void addErrors(final Map errors, final boolean syntactic) {
		if (instance == null) return;
		
		for (final Object at : errors.keySet()) {
			final List list = (List) errors.get(at);
			for (final Object cause : list) {
				final TableItem item = new TableItem(instance.table, SWT.NONE);
				item.setText(0, Integer.toString(((Integer) at).intValue() + 1));
				item.setText(2, cause.toString());
				
				if (syntactic) {
					item.setText(1, "Sintaxis");
				} else {
					item.setText(1, "Sem\u00e1ntica");
				}
			}
		}
	}
	
}
